package com.example.mycarcalling.service.impl;

import com.example.mycarcalling.bean.Navigation;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.dao.NavigationMapper;
import com.example.mycarcalling.dao.OrderMapper;
import com.example.mycarcalling.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("orderService")
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private NavigationMapper navigationMapper;

    @Override
    public Long sendingOrder(Order order) {
        try {
            orderMapper.insertSelective(order);
            return order.getOrderId();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Order getSendingOrder(Long navigationId) {
        if(navigationId == null || navigationId == 0){
            return null;
        }
        Order order = null;
        try {
            order = orderMapper.selectOrderByNavigationId(navigationId);
            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Order getOnDrivingOrder(Long userid) {
        if(userid == null || userid == 0){
            return null;
        }
        Order order = null;
        try {
            order = orderMapper.selectOrdersOnDriving(userid);
            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Order> getCompeteOrders(Long userid) {
        if(userid == null || userid == 0){
            return null;
        }
        List<Order> orders = null;
        try {
            orders = orderMapper.selectOrdersWithCompeting(userid);
            return orders;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Order OrderConfirm(Long navigationId,Long driverId) {
        if(null==navigationId || navigationId == 0){
            return null;
        }
        Navigation navigation = null;
        Order order = null;
        try {
            navigation = navigationMapper.selectByNvIdAndZeroStatus(navigationId);
            if(null == navigation){
                return null;
            }
            navigation.setStatus(1);
            navigationMapper.updateByPrimaryKeySelective(navigation);
            if(navigation.getStatus() == 0) {
                return null;
            }
            order = orderMapper.selectOrderByNavigationId(navigationId);

            order.setDeliveryDriver(driverId);
            order.setOrderStatus(1);

            orderMapper.updateByPrimaryKeySelective(order);
            if(order.getOrderStatus() == 0){
                return null;
            }

            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long pickUpStatus(Long orderId) {
        if(null == orderId){
            return null;
        }
        Order order = new Order();
        try {
            order.setOrderId(orderId);
            order.setOrderStatus(2);
            if(1 == orderMapper.updateByPrimaryKeySelective(order)){
                return order.getOrderId();
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long getOffStatus(Long orderId) {
        if(null == orderId){
            return null;
        }
        Order order = new Order();
        try {
            order.setOrderId(orderId);
            order.setOrderStatus(3);
            order.setCompeteTime(new Date());
            if(1 == orderMapper.updateByPrimaryKeySelective(order)){
                //修改导航状态为2
                Navigation navigation = new Navigation();
                Long navigationId = orderMapper.selectByPrimaryKey(orderId).getNavigationInfo();
                navigation.setNavigationId(navigationId);
                navigation.setStatus(2);
                if(1 == navigationMapper.updateByPrimaryKeySelective(navigation)) {
                    return order.getOrderId();
                }
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Order getReadyCompeteOrder(Long orderId) {
        if(orderId == null || orderId == 0){
            return null;
        }
        Order order = null;
        try {
            order = orderMapper.selectByPrimaryKey(orderId);
            return order;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
